<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>退款详情</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="goBack">返回</el-button>
      </div>
      
      <el-form :model="form" label-width="120px" class="detail-form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="退款单号：">
              <span>{{ form.refundNo }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单号：">
              <span>{{ form.orderNo }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户ID：">
              <span>{{ form.userId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款类型：">
              <dict-tag :options="dict.type.ec_refund_type" :value="form.refundType"/>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="退款原因：">
              <span>{{ form.refundReason }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退款金额：">
              <span>{{ form.refundAmount }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="退款状态：">
              <dict-tag :options="dict.type.ec_refund_status" :value="form.refundStatus"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请时间：">
              <span>{{ parseTime(form.applyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="处理时间：">
              <span>{{ parseTime(form.handleTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户备注：">
              <span>{{ form.handleNote }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="24">
            <el-form-item label="凭证图片：">
              <image-preview :src="form.evidenceImages" :width="100" :height="100"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      
      <!-- 审核按钮 -->
      <div class="audit-buttons" v-if="form.refundStatus === '1'">
        <el-button type="success" @click="handleApprove" :loading="approving">审核通过</el-button>
        <el-button type="danger" @click="handleReject" :loading="rejecting">审核拒绝</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getRefund, updateRefund } from "@/api/fun/refund"
import { updateOrder, updateOrderStatus } from "@/api/fun/order"
import ImagePreview from "@/components/ImagePreview"

export default {
  name: "RefundDetail",
  components: {
    ImagePreview
  },
  dicts: ['ec_refund_status', 'ec_refund_type'],
  data() {
    return {
      // 表单数据
      form: {},
      // 审核中状态
      approving: false,
      rejecting: false
    }
  },
  created() {
    const refundId = this.$route.query.refundId
    if (refundId) {
      this.getRefundDetail(refundId)
    }
  },
  methods: {
    /** 获取退款详情 */
    getRefundDetail(refundId) {
      getRefund(refundId).then(response => {
        this.form = response.data
      })
    },
    /** 审核通过 */
    handleApprove() {
      this.$modal.confirm('确认审核通过该退款申请？').then(() => {
        this.approving = true
        const updateData = {
          refundId: this.form.refundId,
          refundStatus: '2', // 审核通过
          handleTime: new Date()
        }
        
        updateRefund(updateData).then(response => {
          if (response.code === 200) {
            // 同步更新订单状态为已退款
            updateOrderStatus(this.form.orderNo, 6).then(orderResponse => {
              if (orderResponse.code === 200) {
                this.$modal.msgSuccess("审核通过成功，订单状态已同步更新")
                this.form.refundStatus = '2'
                this.form.handleTime = new Date()
              } else {
                this.$modal.msgWarning("退款审核成功，但订单状态更新失败")
              }
            }).catch(() => {
              this.$modal.msgWarning("退款审核成功，但订单状态更新失败")
            })
          } else {
            this.$modal.msgError(response.msg || "审核失败")
          }
        }).catch(() => {
          this.$modal.msgError("审核失败")
        }).finally(() => {
          this.approving = false
        })
      })
    },
    /** 审核拒绝 */
    handleReject() {
      this.$modal.confirm('确认拒绝该退款申请？').then(() => {
        this.rejecting = true
        const updateData = {
          refundId: this.form.refundId,
          refundStatus: '3', // 审核拒绝
          handleTime: new Date()
        }
        
        updateRefund(updateData).then(response => {
          if (response.code === 200) {
            this.$modal.msgSuccess("审核拒绝成功")
            this.form.refundStatus = '3'
            this.form.handleTime = new Date()
          } else {
            this.$modal.msgError(response.msg || "操作失败")
          }
        }).catch(() => {
          this.$modal.msgError("操作失败")
        }).finally(() => {
          this.rejecting = false
        })
      })
    },
    /** 返回 */
    goBack() {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.detail-form {
  margin-top: 20px;
}

.detail-form .el-form-item {
  margin-bottom: 20px;
}

.detail-form .el-form-item__label {
  font-weight: bold;
}

.audit-buttons {
  text-align: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.audit-buttons .el-button {
  margin: 0 10px;
  padding: 12px 30px;
}
</style>